package constant.milk.cashbook.select;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import constant.milk.cashbook.MainActivity;
import constant.milk.cashbook.R;
import constant.milk.cashbook.SuperActivity;
import constant.milk.cashbook.bean.InOutData;
import constant.milk.cashbook.database.DBSelectQuery;
import constant.milk.cashbook.tip.TipMap;
import constant.milk.cashbook.tip.TipInfoActivity;
import constant.milk.cashbook.util.CalendarActivity;
import constant.milk.cashbook.util.MilkManager;
import constant.milk.cashbook.util.MilkTextView;
import constant.milk.cashbook.util.MilkUtil;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class SelectListActivity extends SuperActivity
{
	public static final int REQUEST_INOUT_INFO = 0;
	private static final int DATE1_REQUEST_CODE = 1;
	private static final int DATE2_REQUEST_CODE = 2;
	
	public static final String SELECT_TYPE = "SELECT_TYPE";
	
	public static final int SELECT_OUT = 0;
	public static final int SELECT_IN = 1;
	public static final int SELECT_MOVE = 2;
	public static final int SELECT_FAN = 3;
	public static final int SELECT_CRDITCARD = 4;
	public static final int SELECT_PHONE = 5;

	private View normalView;
	private TextView totalMoneyTextView;
	
	private View allView; 
	private TextView outMoneyTextView;
	private TextView inMoneyTextView;
	private TextView moveMoneyTextView;
	
	private TextView date1TextView;
	private TextView date2TextView;
	
	private View date1View;
	private View date2View;
	
	private ListView listView;
	
	private int year1;
	private int month1;
	private int day1;
	
	private int year2;
	private int month2;
	private int day2;
	
	private ArrayList<InOutData> list;
	
	private MilkManager assetManager;
	private DBSelectQuery select;
	
	private int mainFlag;
	private String mainAsset;
	
	private Intent intent;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.select_list_activity);
		
		setFindViewId();
		setOnClick();
		setData();
	}

	@Override
	protected void setFindViewId()
	{
		normalView = findViewById(R.id.selectListNormalView);
		totalMoneyTextView = (TextView) findViewById(R.id.selectListTotalMoneyTextView);
		
		allView = findViewById(R.id.selectListAllView);
		outMoneyTextView = (TextView) findViewById(R.id.selectListOutMoneyTextView);
		inMoneyTextView = (TextView) findViewById(R.id.selectListInMoneyTextView);
		moveMoneyTextView = (TextView) findViewById(R.id.selectListMoveMoneyTextView);
		
		date1TextView = (TextView) findViewById(R.id.selectListDate1TextView);
		date2TextView = (TextView) findViewById(R.id.selectListDate2TextView);
		
		date1View = findViewById(R.id.selectListDate1View);
		date2View = findViewById(R.id.selectListDate2View);
		
		listView = (ListView) findViewById(R.id.listView);
	}

	@Override
	protected void setOnClick()
	{
		date1View.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, CalendarActivity.class);
				intent.putExtra(CalendarActivity.YEAR, year1);
				intent.putExtra(CalendarActivity.MONTN, month1);
				intent.putExtra(CalendarActivity.DAY, day1);
				startActivityForResult(intent, DATE1_REQUEST_CODE);
			}
		});
		
		date2View.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, CalendarActivity.class);
				intent.putExtra(CalendarActivity.YEAR, year2);
				intent.putExtra(CalendarActivity.MONTN, month2);
				intent.putExtra(CalendarActivity.DAY, day2);
				startActivityForResult(intent, DATE2_REQUEST_CODE);
			}
		});
	}

	@Override
	protected void setData()
	{
		assetManager = new MilkManager(superContext, superDb);
		select = new DBSelectQuery(superContext, superDb);
		
		Calendar cal = Calendar.getInstance();
		
		intent = getIntent();
		
		year1 = intent.getIntExtra(SelectMainActivity.YEAR1_DATA, cal.get(Calendar.YEAR));
		month1 = intent.getIntExtra(SelectMainActivity.MONTH1_DATA, cal.get(Calendar.MONTH));
		day1 = intent.getIntExtra(SelectMainActivity.DAY1_DATA, cal.get(Calendar.DAY_OF_MONTH));
		year2 = intent.getIntExtra(SelectMainActivity.YEAR2_DATA, cal.get(Calendar.YEAR));
		month2 = intent.getIntExtra(SelectMainActivity.MONTH2_DATA, cal.get(Calendar.MONTH));
		day2 = intent.getIntExtra(SelectMainActivity.DAY2_DATA, cal.get(Calendar.DAY_OF_MONTH));
		
		mainFlag = intent.getIntExtra(MainActivity.MAIN_SELECT_FLAG, 0);
		
		date1TextView.setTextSize(TEXT_SIZE - 2);
		date2TextView.setTextSize(TEXT_SIZE - 2);
		
		date1TextView.setText(MilkUtil.getDate(year1, month1+1, day1));
		date2TextView.setText(MilkUtil.getDate(year2, month2+1, day2));
		
		if (mainFlag > 0)
		{
			setMainSelect();
		}
		else
		{
			setSelect();
		}
	}
	
	private void setMainSelect()
	{
		normalView.setVisibility(View.VISIBLE);
		allView.setVisibility(View.GONE);
		
		String type = intent.getStringExtra(MainActivity.MAIN_SELECT_TYPE);
		mainAsset = intent.getStringExtra(MainActivity.MAIN_SELECT_ASSET);
		String name = intent.getStringExtra(MainActivity.MAIN_SELECT_NAME);
		String fan = intent.getStringExtra(MainActivity.MAIN_SELECT_FAN);
		
		if (type.equals("3"))
		{
			list = select.getInOutMainFanList(Integer.parseInt(fan), 
					MilkUtil.getDateDB(year1, month1+1, day1), MilkUtil.getDateDB(year2, month2+1, day2));
		}
		else
		{
			list = select.getInOutMainList(Integer.parseInt(type), Integer.parseInt(mainAsset), name,
					MilkUtil.getDateDB(year1, month1+1, day1),
					MilkUtil.getDateDB(year2, month2+1, day2));
		}
		
		String title = "메인조회목록";
		
		BigDecimal money = BigDecimal.ZERO;
		
		for (int i = 0; i < list.size(); i++)
		{
			if (list.get(i).getType().equals("8"))
			{
				continue;
			}
			
			if (type.equals("0"))
			{
				ArrayList<InOutData> tempList = new ArrayList<InOutData>();
				tempList.add(list.get(i));
				
				if (mainAsset.equals("5"))
				{
					money = money.add(new BigDecimal(assetManager.getSumOutGiftCardMoney(tempList)));
				}
				else
				{
					money = money.add(new BigDecimal(assetManager.getSumInOutMoney(tempList, mainAsset)));
				}
			}
			else
			{
				money = money.add(new BigDecimal(list.get(i).getMoney()));
			}
		}
		
		totalMoneyTextView.setText(MilkUtil.getWon(money.toString()));
		
		listView.setOnItemClickListener(onItemClickListener);
		
		ArrayAdapter<?> adapter = null;
		
		if (type.equals("0"))
		{
			title = "메인지출목록";
			
			if (mainAsset.equals("5"))
			{
				adapter = new InOutGiftCardAdapter(
						superContext, R.layout.select_list_move_item, getList(list));
				listView.setAdapter(adapter);
			}
			else
			{
				adapter = new InOutAdapter(
						superContext, R.layout.select_list_item, getList(list));
				listView.setAdapter(adapter);
			}
		}
		else if (type.equals("1"))
		{
			title = "메인수입목록";
			
			adapter = new InOutAdapter(
					superContext, R.layout.select_list_item, getList(list));
			listView.setAdapter(adapter);
		}
		else if (type.equals("2"))
		{
			title = "메인이동목록";
			
			if (mainAsset.equals("5"))
			{
				adapter = new InOutGiftCardAdapter(
						superContext, R.layout.select_list_move_item, getList(list));
				listView.setAdapter(adapter);
			}
			else
			{
				adapter = new InOutMoveAdapter(
						superContext, R.layout.select_list_move_item, getList(list));
				listView.setAdapter(adapter);
			}
		}
		else if (type.equals("3"))
		{
			title = "메인부채목록";
			
			adapter = new InOutFanAdapter(
					superContext, R.layout.select_list_move_item, getList(list));
			listView.setAdapter(adapter);
		}
		
		setTitle(title);
	}
	
	private void setSelect()
	{
		int typeFlag = intent.getIntExtra(SelectMainActivity.TYPE_FLAG, 0);
		int kindFlag = intent.getIntExtra(SelectMainActivity.KIND_FLAG, 0);
		String asset = intent.getStringExtra(SelectMainActivity.ASSET_FLAG);
		String name = intent.getStringExtra(SelectMainActivity.NAME_FLAG);
		String mainCategory = intent.getStringExtra(SelectMainActivity.MAIN_CATAGORY);
		String subCategory = intent.getStringExtra(SelectMainActivity.SUB_CATAGORY);
		String memo = intent.getStringExtra(SelectMainActivity.MEMO_FLAG);
		
		if (typeFlag == 3)
		{
			if (kindFlag < 3)
			{
				list = select.getInOutListFan(kindFlag, assetManager.getAssetStr(asset), name, mainCategory, subCategory, memo,
						MilkUtil.getDateDB(year1, month1+1, day1),
						MilkUtil.getDateDB(year2, month2+1, day2));
			}
			else
			{
				String id = intent.getStringExtra(SelectInfoFanActivity.INOUT_INFO_FAN_ID);
				
				list = select.getInOutListFanLinkId(kindFlag, id);
			}
		}
		else
		{
			list = select.getInOutList(typeFlag, assetManager.getAssetStr(asset), name, 
					mainCategory, subCategory, memo,
					MilkUtil.getDateDB(year1, month1+1, day1),
					MilkUtil.getDateDB(year2, month2+1, day2));
		}
		
		if (typeFlag == -1)
		{
			normalView.setVisibility(View.GONE);
			allView.setVisibility(View.VISIBLE);
			
			BigDecimal outMoney = BigDecimal.ZERO;
			BigDecimal inMoney = BigDecimal.ZERO;
			BigDecimal moveMoney = BigDecimal.ZERO;
			
			for (int i = 0; i < list.size(); i++)
			{
				InOutData data = list.get(i);
				
				if (data.getType().equals("8") || data.getType().equals("3"))
				{
					continue;
				}
				
				if (data.getType().equals("1"))
				{
					inMoney = assetManager.getSumInOutMoney(data, inMoney);
				}
				else if (data.getType().equals("2") || data.getType().equals("7"))
				{
					moveMoney = assetManager.getSumInOutMoney(data, moveMoney);
				}
				else
				{
					outMoney = assetManager.getSumInOutMoney(data, outMoney);
				}
			}
			
			outMoneyTextView.setTextColor(getResources().getColor(R.color.outColor));
			inMoneyTextView.setTextColor(getResources().getColor(R.color.inColor));
			moveMoneyTextView.setTextColor(getResources().getColor(R.color.moveColor));
			
			outMoneyTextView.setText(MilkUtil.getWon(outMoney.toString()));
			inMoneyTextView.setText(MilkUtil.getWon(inMoney.toString()));
			moveMoneyTextView.setText(MilkUtil.getWon(moveMoney.toString()));
		}
		else
		{
			normalView.setVisibility(View.VISIBLE);
			allView.setVisibility(View.GONE);
			
			BigDecimal money = BigDecimal.ZERO;
			
			for (int i = 0; i < list.size(); i++)
			{
				if (list.get(i).getType().equals("8"))
				{
					continue;
				}
				
				money = assetManager.getSumInOutMoney(list.get(i), money);
			}
			
			totalMoneyTextView.setText(MilkUtil.getWon(money.toString()));
		}
		
		listView.setOnItemClickListener(onItemClickListener);
		
		ArrayAdapter<?> adapter = null;
		
		if (typeFlag == 0)
		{
			setTitle(getString(R.string.out) + "목록");
			
			adapter = new InOutAdapter(
					superContext, R.layout.select_list_item, getList(list));
			listView.setAdapter(adapter);
		}
		else if (typeFlag == 1)
		{
			setTitle(getString(R.string.in) + "목록");
			
			adapter = new InOutAdapter(
					superContext, R.layout.select_list_item, getList(list));
			listView.setAdapter(adapter);
		}
		else if (typeFlag == 2)
		{
			setTitle(getString(R.string.move) + "목록");
			
			adapter = new InOutMoveAdapter(
					superContext, R.layout.select_list_move_item, getList(list));
			listView.setAdapter(adapter);
		}
		else if (typeFlag == 3)
		{
			if (kindFlag == 1)
			{
				setTitle(getString(R.string.fanInlist));
			}
			if (kindFlag == 2)
			{
				setTitle(getString(R.string.fanOutlist));
			}
			if (kindFlag == 3)
			{
				setTitle(getString(R.string.fanReturnlist));
			}
			if (kindFlag == 4)
			{
				setTitle(getString(R.string.fanReceivelist));
			}
			
			adapter = new InOutFanAdapter(
					superContext, R.layout.select_list_move_item, getList(list));
			listView.setAdapter(adapter);
		}
		else if (typeFlag == 4)
		{
			setTitle(getString(R.string.creditCardAsset) + "목록");
			
			adapter = new InOutAdapter(
					superContext, R.layout.select_list_item, getList(list));
			listView.setAdapter(adapter);
		}
		else if (typeFlag == 5)
		{
			setTitle(getString(R.string.phoneAsset) + "목록");
			
			adapter = new InOutAdapter(
					superContext, R.layout.select_list_item, getList(list));
			listView.setAdapter(adapter);
		}
		else if (typeFlag == 6)
		{
			setTitle(getString(R.string.giftCardOut));
			
			adapter = new InOutGiftCardAdapter(
					superContext, R.layout.select_list_move_item, getList(list));
			listView.setAdapter(adapter);
		}
		else if (typeFlag == 7)
		{
			setTitle(getString(R.string.giftCardIn));
			
			adapter = new InOutGiftCardAdapter(
					superContext, R.layout.select_list_move_item, getList(list));
			listView.setAdapter(adapter);
		}
		else if (typeFlag == -1)
		{
			setTitle(getString(R.string.all));
			
			adapter = new InOutAllAdapter(
					superContext, R.layout.select_list_all_item, getAllList(list));
			listView.setAdapter(adapter);
		}
	}

	private ArrayList<InOutData> getList(ArrayList<InOutData> list)
	{
		ArrayList<InOutData> inOutList = new ArrayList<InOutData>();
		
		String date = "0";
		
		for (int i = 0; i < list.size(); i++)
		{
			InOutData data = list.get(i);
			
			if (!date.equals(data.getDate()))
			{
				date = data.getDate();
				
				InOutData totalData = new InOutData();
				
				totalData.setDate(data.getDate());
				
				BigDecimal money = BigDecimal.ZERO;
				
				for (int j = 0; j < list.size(); j++)
				{
					if (!date.equals(list.get(j).getDate()))
					{
						continue;
					}
					
					if (!list.get(j).getType().equals("8"))
					{
						if (mainFlag > 0)
						{
							ArrayList<InOutData> tempList = new ArrayList<InOutData>();
							tempList.add(list.get(j));
							
							if (mainAsset.equals("5"))
							{
								money = money.add(new BigDecimal(assetManager.getSumOutGiftCardMoney(tempList)));
							}
							else
							{
								money = money.add(new BigDecimal(assetManager.getSumInOutMoney(tempList, mainAsset)));
							}
						}
						else
						{
							money = assetManager.getSumInOutMoney(list.get(j), money);
						}
					}
				}
				
				totalData.setMoney(money.toString());
				totalData.setTotalFlag(0);
				
				inOutList.add(totalData);
			}
			
			data.setTotalFlag(1);
			inOutList.add(data);
		}
		
		return inOutList;
	}
	
	private ArrayList<InOutData> getAllList(ArrayList<InOutData> list)
	{
		ArrayList<InOutData> inOutList = new ArrayList<InOutData>();
		
		String date = "0";
		
		for (int i = 0; i < list.size(); i++)
		{
			InOutData data = list.get(i);
			
			if (!date.equals(data.getDate()))
			{
				date = data.getDate();
				
				InOutData totalData = new InOutData();
				
				totalData.setDate(data.getDate());
				
				BigDecimal outMoney = BigDecimal.ZERO;
				BigDecimal inMoney = BigDecimal.ZERO;
				BigDecimal moveMoney = BigDecimal.ZERO;
				
				for (int j = 0; j < list.size(); j++)
				{
					if (!date.equals(list.get(j).getDate()))
					{
						continue;
					}
					
					if (list.get(j).getType().equals("8") || list.get(j).getType().equals("3"))
					{
						continue;
					}
					
					if (list.get(j).getType().equals("1"))
					{
						inMoney = assetManager.getSumInOutMoney(list.get(j), inMoney);
					}
					else if (list.get(j).getType().equals("2") || list.get(j).getType().equals("7"))
					{
						moveMoney = assetManager.getSumInOutMoney(list.get(j), moveMoney);
					}
					else
					{
						outMoney = assetManager.getSumInOutMoney(list.get(j), outMoney);
					}
				}
				
				totalData.setOutMoney(outMoney.toString());
				totalData.setInMoney(inMoney.toString());
				totalData.setMoveMoney(moveMoney.toString());
				totalData.setTotalFlag(0);
				
				inOutList.add(totalData);
			}
			
			data.setTotalFlag(1);
			inOutList.add(data);
		}
		
		return inOutList;
	}
	
	private OnItemClickListener onItemClickListener = new OnItemClickListener()
	{
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3)
		{
			InOutData data = (InOutData) arg0.getItemAtPosition(position);
			
			if (data.getTotalFlag() == 0)
			{
				return;
			}
			
			Intent intent = null;
			
			if (data.getType().equals("2"))
			{
				intent = new Intent(superContext, SelectInfoMoveActivity.class);
				intent.putExtra(SelectInfoMoveActivity.INOUT_INFO_MOVE_ID, data.getId());
			}
			else if (data.getType().equals("3"))
			{
				intent = new Intent(superContext, SelectInfoFanActivity.class);
				intent.putExtra(SelectInfoFanActivity.INOUT_INFO_FAN_ID, data.getId());
			}
			else if (data.getType().equals("6") || data.getType().equals("7"))
			{
				intent = new Intent(superContext, SelectInfoGiftCardActivity.class);
				intent.putExtra(SelectInfoGiftCardActivity.INOUT_INFO_GIFTCARD_ID, data.getId());
			}
			else if (data.getType().equals("8"))
			{
				intent = new Intent(superContext, SelectInfoResultActivity.class);
				intent.putExtra(SelectInfoResultActivity.INOUT_INFO_RESULT_ID, data.getId());
			}
			else
			{	
				intent = new Intent(superContext, SelectInfoActivity.class);
				intent.putExtra(SelectInfoActivity.INOUT_INFO_ID, data.getId());
				intent.putExtra(SELECT_TYPE, Integer.parseInt(data.getType()));
			}
			
			startActivityForResult(intent, REQUEST_INOUT_INFO);
		}
	};
	
	public class InOutAdapter extends ArrayAdapter<InOutData>
	{
		public InOutAdapter(Context context, int textViewResourceId,
				List<InOutData> objects)
		{
			super(context, textViewResourceId, objects);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			View v = convertView;
			
			if (v == null) 
			{
				LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = inflater.inflate(R.layout.select_list_item, null);
			}
			
			InOutData data = getItem(position);
			
			View totalView = v.findViewById(R.id.selectItemTotalView);
			MilkTextView totalTextView = (MilkTextView) v.findViewById(R.id.selectItemTotalTextView);
			MilkTextView dateTextView = (MilkTextView) v.findViewById(R.id.selectItemDateTextView);
			
			View dataView = v.findViewById(R.id.selectItemDataView);
			MilkTextView assetTextView = (MilkTextView) v.findViewById(R.id.selectItemAssetTextView);
			MilkTextView categoryTextView = (MilkTextView) v.findViewById(R.id.selectItemCategoryTextView);
			MilkTextView moneyTextView = (MilkTextView) v.findViewById(R.id.selectItemMoneyTextView);
			
			if (data.getTotalFlag() == 0)
			{
				totalView.setVisibility(View.VISIBLE);
				dataView.setVisibility(View.GONE);
				((MilkTextView) v.findViewById(R.id.selectItemTotalTitleTextView)).setTextSize(TEXT_SIZE);
				dateTextView.setTextSize(TEXT_SIZE);
				totalTextView.setTextSize(TEXT_SIZE);
				
				dateTextView.setText(MilkUtil.getDateButton(data.getDate()));
				totalTextView.setText(MilkUtil.getWon(data.getMoney()));
			}
			else
			{
				totalView.setVisibility(View.GONE);
				dataView.setVisibility(View.VISIBLE);
				assetTextView.setTextSize(TEXT_SIZE);
				categoryTextView.setTextSize(TEXT_SIZE);
				moneyTextView.setTextSize(TEXT_SIZE);
				
				if (data.getType().equals("8"))
				{
					assetTextView.setText(getString(R.string.costResult));
				}
				else
				{
					assetTextView.setText(assetManager.getAssetId(data.getAsset()));
				}
				
				categoryTextView.setText(data.getMaincategory());
				
				if (mainFlag > 0)
				{
					BigDecimal money = BigDecimal.ZERO;
					
					ArrayList<InOutData> tempList = new ArrayList<InOutData>();
					tempList.add(data);
					
					if (mainAsset.equals("5"))
					{
						money = money.add(new BigDecimal(assetManager.getSumOutGiftCardMoney(tempList)));
					}
					else
					{
						money = money.add(new BigDecimal(assetManager.getSumInOutMoney(tempList, mainAsset)));
					}
					
					moneyTextView.setText(MilkUtil.getWon(money.toString()));
				}
				else
				{
					moneyTextView.setText(MilkUtil.getWon(assetManager.getSumInOutMoney(data, BigDecimal.ZERO).toString()));
				}
				
			}
			
			return v;
		}
	}
	
	public class InOutMoveAdapter extends ArrayAdapter<InOutData>
	{
		public InOutMoveAdapter(Context context, int textViewResourceId,
				List<InOutData> objects)
		{
			super(context, textViewResourceId, objects);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			View v = convertView;
			
			if (v == null) 
			{
				LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = inflater.inflate(R.layout.select_list_move_item, null);
			}
			
			InOutData data = getItem(position);
			
			View totalView = v.findViewById(R.id.selectItemMoveTotalView);
			MilkTextView dateTextView = (MilkTextView) v.findViewById(R.id.selectItemMoveDateTextView);
			MilkTextView totalTextView = (MilkTextView) v.findViewById(R.id.selectItemMoveTotalTextView);
			
			View data1View = v.findViewById(R.id.selectItemMoveData1View);
			MilkTextView asset1TextView = (MilkTextView) v.findViewById(R.id.selectItemMoveAsset1TextView);
			MilkTextView category1TextView = (MilkTextView) v.findViewById(R.id.selectItemMoveCategory1TextView);
			MilkTextView money1TextView = (MilkTextView) v.findViewById(R.id.selectItemMoveMoney1TextView);
			
			View data2View = v.findViewById(R.id.selectItemMoveData2View);
			MilkTextView asset2TextView = (MilkTextView) v.findViewById(R.id.selectItemMoveAsset2TextView);
			MilkTextView category2TextView = (MilkTextView) v.findViewById(R.id.selectItemMoveCategory2TextView);
			MilkTextView money2TextView = (MilkTextView) v.findViewById(R.id.selectItemMoveMoney2TextView);
			
			if (data.getTotalFlag() == 0)
			{
				totalView.setVisibility(View.VISIBLE);
				data1View.setVisibility(View.GONE);
				data2View.setVisibility(View.GONE);
				((MilkTextView) v.findViewById(R.id.selectItemMoveTotalTitleTextView)).setTextSize(TEXT_SIZE);
				dateTextView.setTextSize(TEXT_SIZE);
				totalTextView.setTextSize(TEXT_SIZE);
				
				dateTextView.setText(MilkUtil.getDateButton(data.getDate()));
				totalTextView.setText(MilkUtil.getWon(data.getMoney()));
			}
			else
			{
				totalView.setVisibility(View.GONE);
				data1View.setVisibility(View.VISIBLE);
				data2View.setVisibility(View.VISIBLE);
				
				asset1TextView.setTextSize(TEXT_SIZE);
				category1TextView.setTextSize(TEXT_SIZE);
				money1TextView.setTextSize(TEXT_SIZE);
				
				asset2TextView.setTextSize(TEXT_SIZE);
				category2TextView.setTextSize(TEXT_SIZE);
				money2TextView.setTextSize(TEXT_SIZE);
				
				if (data.getType().equals("7"))
				{
					asset1TextView.setText(assetManager.getAssetId(data.getSubasset()));
					category1TextView.setText(getString(R.string.giftCardIn));
					money1TextView.setText(MilkUtil.getWon(data.getMoney()));
					asset2TextView.setText(assetManager.getAssetId(data.getAsset()));
					category2TextView.setText(data.getMaincategory());
					money2TextView.setText(MilkUtil.getWon(data.getMoney()));
				}
				else
				{
					asset1TextView.setText(assetManager.getAssetId(data.getAsset()));
					category1TextView.setText(data.getMaincategory());
					money1TextView.setText(MilkUtil.getWon(data.getMoney()));
					asset2TextView.setText(assetManager.getAssetId(data.getSubasset()));
					category2TextView.setText(data.getMaincategory());
					money2TextView.setText(MilkUtil.getWon(data.getMoney()));
				}
			}
			
			return v;
		}
	}
	
	public class InOutFanAdapter extends ArrayAdapter<InOutData>
	{
		public InOutFanAdapter(Context context, int textViewResourceId,
				List<InOutData> objects)
		{
			super(context, textViewResourceId, objects);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			View v = convertView;
			
			if (v == null) 
			{
				LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = inflater.inflate(R.layout.select_list_item, null);
			}
			
			InOutData data = getItem(position);
			
			View totalView = v.findViewById(R.id.selectItemTotalView);
			MilkTextView dateTextView = (MilkTextView) v.findViewById(R.id.selectItemDateTextView);
			MilkTextView totalTextView = (MilkTextView) v.findViewById(R.id.selectItemTotalTextView);
			
			View dataView = v.findViewById(R.id.selectItemDataView); 
			MilkTextView assetTextView = (MilkTextView) v.findViewById(R.id.selectItemAssetTextView);
			MilkTextView categoryTextView = (MilkTextView) v.findViewById(R.id.selectItemCategoryTextView);
			MilkTextView moneyTextView = (MilkTextView) v.findViewById(R.id.selectItemMoneyTextView);
			
			if (data.getTotalFlag() == 0)
			{
				totalView.setVisibility(View.VISIBLE);
				dataView.setVisibility(View.GONE);
				((MilkTextView) v.findViewById(R.id.selectItemTotalTitleTextView)).setTextSize(TEXT_SIZE);
				dateTextView.setTextSize(TEXT_SIZE);
				totalTextView.setTextSize(TEXT_SIZE);
				
				dateTextView.setText(MilkUtil.getDateButton(data.getDate()));
				totalTextView.setText(MilkUtil.getWon(data.getMoney()));
			}
			else
			{
				totalView.setVisibility(View.GONE);
				dataView.setVisibility(View.VISIBLE);
				assetTextView.setTextSize(TEXT_SIZE);
				categoryTextView.setTextSize(TEXT_SIZE);
				moneyTextView.setTextSize(TEXT_SIZE);
				
				assetTextView.setText(assetManager.getAssetId(data.getAsset()));
				categoryTextView.setText(data.getPerson());
				moneyTextView.setText(MilkUtil.getWon(data.getMoney()));
			}
			
			return v;
		}
	}
	
	public class InOutGiftCardAdapter extends ArrayAdapter<InOutData>
	{
		public InOutGiftCardAdapter(Context context, int textViewResourceId,
				List<InOutData> objects)
		{
			super(context, textViewResourceId, objects);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			View v = convertView;
			
			if (v == null) 
			{
				LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = inflater.inflate(R.layout.select_list_move_item, null);
			}
			
			InOutData data = getItem(position);
			
			View totalView = v.findViewById(R.id.selectItemMoveTotalView);
			MilkTextView dateTextView = (MilkTextView) v.findViewById(R.id.selectItemMoveDateTextView);
			MilkTextView totalTextView = (MilkTextView) v.findViewById(R.id.selectItemMoveTotalTextView);
			
			View data1View = v.findViewById(R.id.selectItemMoveData1View);
			MilkTextView asset1TextView = (MilkTextView) v.findViewById(R.id.selectItemMoveAsset1TextView);
			MilkTextView category1TextView = (MilkTextView) v.findViewById(R.id.selectItemMoveCategory1TextView);
			MilkTextView money1TextView = (MilkTextView) v.findViewById(R.id.selectItemMoveMoney1TextView);
			
			View data2View = v.findViewById(R.id.selectItemMoveData2View);
			MilkTextView asset2TextView = (MilkTextView) v.findViewById(R.id.selectItemMoveAsset2TextView);
			MilkTextView category2TextView = (MilkTextView) v.findViewById(R.id.selectItemMoveCategory2TextView);
			MilkTextView money2TextView = (MilkTextView) v.findViewById(R.id.selectItemMoveMoney2TextView);
			
			if (data.getTotalFlag() == 0)
			{
				totalView.setVisibility(View.VISIBLE);
				data1View.setVisibility(View.GONE);
				data2View.setVisibility(View.GONE);
				((MilkTextView) v.findViewById(R.id.selectItemMoveTotalTitleTextView)).setTextSize(TEXT_SIZE);
				dateTextView.setTextSize(TEXT_SIZE);
				totalTextView.setTextSize(TEXT_SIZE);
				
				dateTextView.setText(MilkUtil.getDateButton(data.getDate()));
				totalTextView.setText(MilkUtil.getWon(data.getMoney()));
			}
			else
			{
				totalView.setVisibility(View.GONE);
				data1View.setVisibility(View.VISIBLE);
				data2View.setVisibility(View.VISIBLE);
				
				asset1TextView.setTextSize(TEXT_SIZE);
				category1TextView.setTextSize(TEXT_SIZE);
				money1TextView.setTextSize(TEXT_SIZE);
				
				asset2TextView.setTextSize(TEXT_SIZE);
				category2TextView.setTextSize(TEXT_SIZE);
				money2TextView.setTextSize(TEXT_SIZE);
				
				if (data.getType().equals("6"))
				{
					asset1TextView.setText(assetManager.getAssetId(data.getAsset()));
					category1TextView.setText(data.getMaincategory());
					money1TextView.setText(MilkUtil.getWon(data.getMoney()));
					
					if (data.getKind().equals("5"))
					{
						asset2TextView.setText(getString(R.string.fullMoney));
						category2TextView.setText("");
						money2TextView.setText("");
					}
					else
					{
						asset2TextView.setText(assetManager.getAssetId(data.getSubasset()));
						
						if (data.getKind().equals("6"))
						{
							category2TextView.setText(getString(R.string.addMoney));
						}
						else if (data.getKind().equals("7"))
						{
							category2TextView.setText(getString(R.string.changeMoney));
						}
						
						money2TextView.setText(MilkUtil.getWon(data.getSubmoney()));
					}
				}
				else
				{
					asset1TextView.setText(assetManager.getAssetId(data.getSubasset()));
					category1TextView.setText(getString(R.string.giftCardIn));
					money1TextView.setText(MilkUtil.getWon(data.getMoney()));
					
					asset2TextView.setText(assetManager.getAssetId(data.getAsset()));
					category2TextView.setText(data.getMaincategory());
					money2TextView.setText(MilkUtil.getWon(data.getMoney()));
				}
			}
			
			return v;
		}
	}
	public class InOutAllAdapter extends ArrayAdapter<InOutData>
	{

		public InOutAllAdapter(Context context, int textViewResourceId,
				List<InOutData> objects)
		{
			super(context, textViewResourceId, objects);
		}
	
		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			View v = convertView;
			
			if (v == null) 
			{
				LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = inflater.inflate(R.layout.select_list_all_item, null);
			}
			
			InOutData data = getItem(position);
			
			View totalView = v.findViewById(R.id.selectItemAllTotalView);
			MilkTextView dateTextView = (MilkTextView) v.findViewById(R.id.selectItemAllDateTextView);
			MilkTextView outTextView = (MilkTextView) v.findViewById(R.id.selectItemAllOutTextView);
			MilkTextView inTextView = (MilkTextView) v.findViewById(R.id.selectItemAllInTextView);
			MilkTextView moveTextView = (MilkTextView) v.findViewById(R.id.selectItemAllMoveTextView);
			
			View data1View = v.findViewById(R.id.selectItemAllData1View);
			MilkTextView type1TextView = (MilkTextView) v.findViewById(R.id.selectItemAllType1TextView);
			MilkTextView asset1TextView = (MilkTextView) v.findViewById(R.id.selectItemAllAsset1TextView);
			MilkTextView money1TextView = (MilkTextView) v.findViewById(R.id.selectItemAllMoney1TextView);
			
			View data2View = v.findViewById(R.id.selectItemAllData2View);
			MilkTextView type2TextView = (MilkTextView) v.findViewById(R.id.selectItemAllType2TextView);
			MilkTextView asset2TextView = (MilkTextView) v.findViewById(R.id.selectItemAllAsset2TextView);
			MilkTextView money2TextView = (MilkTextView) v.findViewById(R.id.selectItemAllMoney2TextView);
			
			dateTextView.setTextSize(TEXT_SIZE);
			outTextView.setTextSize(TEXT_SIZE);
			inTextView.setTextSize(TEXT_SIZE);
			moveTextView.setTextSize(TEXT_SIZE);
			type1TextView.setTextSize(TEXT_SIZE);
			asset1TextView.setTextSize(TEXT_SIZE);
			money1TextView.setTextSize(TEXT_SIZE);
			type2TextView.setTextSize(TEXT_SIZE);
			asset2TextView.setTextSize(TEXT_SIZE);
			money2TextView.setTextSize(TEXT_SIZE);
			
			if (data.getTotalFlag() == 0)
			{
				totalView.setVisibility(View.VISIBLE);
				data1View.setVisibility(View.GONE);
				data2View.setVisibility(View.GONE);
				
				outTextView.setTextColor(getResources().getColor(R.color.outColor));
				inTextView.setTextColor(getResources().getColor(R.color.inColor));
				moveTextView.setTextColor(getResources().getColor(R.color.moveColor));
				
				dateTextView.setText(MilkUtil.getDateButton(data.getDate()));
				outTextView.setText(MilkUtil.getWon(data.getOutMoney()));
				inTextView.setText(MilkUtil.getWon(data.getInMoney()));
				moveTextView.setText(MilkUtil.getWon(data.getMoveMoney()));
			}
			else
			{
				totalView.setVisibility(View.GONE);
				data1View.setVisibility(View.VISIBLE);
				
				if (data.getType().equals("2") || data.getType().equals("7"))
				{
					data2View.setVisibility(View.VISIBLE);
					money1TextView.setTextColor(getResources().getColor(R.color.moveColor));
					money2TextView.setTextColor(getResources().getColor(R.color.moveColor));
					
					if (data.getType().equals("2"))
					{
						type1TextView.setText(getString(R.string.outAsset));
						asset1TextView.setText(assetManager.getAssetId(data.getAsset()));
						money1TextView.setText(MilkUtil.getWon(data.getMoney()));
						
						type2TextView.setText(getString(R.string.inAsset));
						asset2TextView.setText(assetManager.getAssetId(data.getSubasset()));
						
						if (!data.getSusumoney().equals(data.getMoney()) && data.getSubmoney().equals("0"))
						{
							money2TextView.setText(MilkUtil.getWon(data.getMoney()));
						}
						else
						{
							money2TextView.setText(MilkUtil.getWon(data.getSubmoney()));
						}
					}
					else
					{
						type1TextView.setText(assetManager.getExcelKind(data.getKind(), data.getType()));
						asset1TextView.setText(assetManager.getAssetId(data.getAsset()));
						money1TextView.setText(MilkUtil.getWon(data.getMoney()));
						
						type2TextView.setText(assetManager.getAssetId(data.getSubasset()));
						asset2TextView.setText(data.getSubname());
						money2TextView.setText(MilkUtil.getWon(data.getSubmoney()));
					}
				}
				else if (data.getType().equals("1"))
				{
					data2View.setVisibility(View.GONE);
					money1TextView.setTextColor(getResources().getColor(R.color.inColor));
					
					type1TextView.setText(assetManager.getExcelKind(data.getKind(), data.getType()));
					asset1TextView.setText(assetManager.getAssetId(data.getAsset()));
					money1TextView.setText(MilkUtil.getWon(data.getMoney()));
				}
				else
				{
					data2View.setVisibility(View.GONE);
					
					if (data.getType().equals("3"))
					{
						money1TextView.setTextColor(TEXT_COLOR);
					}
					else
					{
						money1TextView.setTextColor(getResources().getColor(R.color.outColor));
					}
					
					type1TextView.setText(assetManager.getExcelKind(data.getKind(), data.getType()));
					asset1TextView.setText(assetManager.getAssetId(data.getAsset()));
					money1TextView.setText(MilkUtil.getWon(data.getMoney()));
				}
			}
			
			return v;
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);
		
		if (resultCode != Activity.RESULT_OK)
		{
			return;
		}
		
		if (requestCode == REQUEST_INOUT_INFO)
		{
			setData();
		}
		else if (requestCode == DATE1_REQUEST_CODE)
		{
			Calendar cal = Calendar.getInstance();
			int tyear1 = data.getIntExtra(CalendarActivity.YEAR, cal.get(Calendar.YEAR));
			int tmonth1 = data.getIntExtra(CalendarActivity.MONTN, cal.get(Calendar.MONTH));
			int tday1 = data.getIntExtra(CalendarActivity.DAY, cal.get(Calendar.DAY_OF_MONTH));
			
			int date1 = Integer.parseInt(MilkUtil.getDateDB(tyear1, tmonth1, tday1));
			int date2 = Integer.parseInt(MilkUtil.getDateDB(year2, month2, day2));
			
			if (!setDateCheck(date1, date2))
			{
				return;
			}
			
			year1 = data.getIntExtra(CalendarActivity.YEAR, cal.get(Calendar.YEAR));
			month1 = data.getIntExtra(CalendarActivity.MONTN, cal.get(Calendar.MONTH));
			day1 = data.getIntExtra(CalendarActivity.DAY, cal.get(Calendar.DAY_OF_MONTH));
			
			date1TextView.setText(MilkUtil.getDate(year1, month1+1, day1));
			
			if (mainFlag > 0)
			{
				setMainSelect();
			}
			else
			{
				setSelect();
			}
		}
		else if (requestCode == DATE2_REQUEST_CODE)
		{
			Calendar cal = Calendar.getInstance();
			int tyear2 = data.getIntExtra(CalendarActivity.YEAR, cal.get(Calendar.YEAR));
			int tmonth2 = data.getIntExtra(CalendarActivity.MONTN, cal.get(Calendar.MONTH));
			int tday2 = data.getIntExtra(CalendarActivity.DAY, cal.get(Calendar.DAY_OF_MONTH));
			
			int date1 = Integer.parseInt(MilkUtil.getDateDB(year1, month1, day1));
			int date2 = Integer.parseInt(MilkUtil.getDateDB(tyear2, tmonth2, tday2));
			
			if (!setDateCheck(date1, date2))
			{
				return;
			}
			
			year2 = data.getIntExtra(CalendarActivity.YEAR, cal.get(Calendar.YEAR));
			month2 = data.getIntExtra(CalendarActivity.MONTN, cal.get(Calendar.MONTH));
			day2 = data.getIntExtra(CalendarActivity.DAY, cal.get(Calendar.DAY_OF_MONTH));
			
			date2TextView.setText(MilkUtil.getDate(year2, month2+1, day2));
			
			if (mainFlag > 0)
			{
				setMainSelect();
			}
			else
			{
				setSelect();
			}
		}
	}
	
	private boolean setDateCheck(int date1, int date2)
	{
		if (date1 > date2)
		{
			MilkUtil.showToast(superContext, "앞에 날짜가 뒤에 날짜보다 클수 없습니다.");
			return false;
		}
		
		return true;
	}

	@Override
	protected void tipButton()
	{
		Intent intent = new Intent(superContext, TipInfoActivity.class);
		intent.putExtra(TipInfoActivity.TIP_ID, TipMap.TIP_SELECT_LIST);
		startActivity(intent);
	}

}
